<?php

namespace haibao\www\business\message;
use haibao\www\model\data\message\MessageBox as MessageBoxModel;
use haibao\www\model\data\message\MessageBoxUnReg as MessageBoxUnRegModel;
use haibao\www\data\message\MessageBox as MessageBoxData;
use haibao\www\data\message\MessageBoxUnReg as MessageBoxUnRegData;
use haibao\comment\model\data\CommentType as CommentTypeModel;
use haibao\www\business\message\MessageUser as MessageUserBusiness;
use haibao\www\model\filter\message\MessageBox as MessageBoxFilter;
use haibao\www\model\filter\message\MessageBoxUnReg as MessageBoxUnRegFilter;
use haibao\www\business\message\MessageSystem as MessageSystemBusiness;
use haibao\frame\data\query\Condition;
use haibao\comment\common\Auth;
use haibao\www\business\BusinessException;
use haibao\cms\data\AmericanTag;
use \haibao\www\model\data\Favorite as FavoriteModel;
use \haibao\user\model\data\UserDynamics as UserDynamicsModel;
class MessageBox extends \haibao\www\business\Base{

	const MESSAGE_DATA_MESSAGE_TYPE_SELF_COMMENT = 1;
	const MESSAGE_DATA_MESSAGE_TYPE_REPLY_COMMENT = 2;
	const MESSAGE_DATA_MESSAGE_TYPE_PARISE_COMMENT = 3;
	const MESSAGE_DATA_MESSAGE_TYPE_BBS_COMMENT = 4;
	const MESSAGE_DATA_MESSAGE_TYPE_BLOG_COMMENT = 5;
	const MESSAGE_DATA_MESSAGE_TYPE_ATTENTION_USER = 6;
	const MESSAGE_DATA_MESSAGE_TYPE_FAVORITE_BBS = 7;
	const MESSAGE_DATA_MESSAGE_TYPE_FAVORITE_BLOG = 8;
	const MESSAGE_DATA_MESSAGE_TYPE_GUESTBOOK = 9;
	const MESSAGE_LIMIT = 15;
	private static $userInfo = array();
	private static $messageBoxData = null;
	private static $isPc = true;
	private static $togTags;
	private static $objectData = array();
	public static function addCommentMessage($commentModel,$commentMessageType=self::MESSAGE_DATA_MESSAGE_TYPE_SELF_COMMENT,$userInfo=array()){

		self::$userInfo = $userInfo;
		$isTrueUser = true;
		if($commentMessageType == self::MESSAGE_DATA_MESSAGE_TYPE_REPLY_COMMENT){
			if(strlen($commentModel->ReplyUserId) == 36){
				$isTrueUser = false;
			}
		}else{
			if(!$commentModel->UserId){
				$isTrueUser = false;
			}
		}
		self::pushCommentMessage($commentModel,$commentMessageType,$isTrueUser);
		
	}
	
	public static function pushCommentMessage($commentModel,$commentMessageType,$isTrueUser = true){
		$messageBoxModel = self::packCommentMessageBoxModel($commentModel,$commentMessageType,$isTrueUser);
		if($isTrueUser){
			$data = new MessageBoxData;
		}else{
			$data = new MessageBoxUnRegData;
		}
		$data->add($messageBoxModel);
		MessageUserBusiness::updateMessageUser($messageBoxModel->ToUserId,MessageBoxModel::MESSAGE_TYPE_COMMENT,$isTrueUser,1);
	}
	
	
	private static function packCommentMessageBoxModel($commentModel,$messageDataType,$isTrueUser){
		if($isTrueUser){
			$messageBoxModel = new MessageBoxModel();
		}else{
			$messageBoxModel = new MessageBoxUnRegModel();
		}
		$messageBoxModel->MessageType = MessageBoxModel::MESSAGE_TYPE_COMMENT;
	
		$messageData = array();
		$messageData['IsTrueUser'] = $isTrueUser;
		$messageData['MessageType'] = $messageDataType;
		$messageData['ObjectId'] = $commentModel->ObjectId;
		$messageData['CommentsTypeCode'] = $commentModel->CommentsTypeCode;
	
		$messageDataContent = self::formatCommentContent($commentModel->MyComment);
		switch($messageDataType){
			case self::MESSAGE_DATA_MESSAGE_TYPE_SELF_COMMENT:
				$messageBoxModel->FromUserId = NULL;
				if($isTrueUser){
					if(self::$userInfo['sso_user_public'] == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
						$messageBoxModel->FromUserId = self::$userInfo['_auth_user_id'];
					}
					$messageBoxModel->ToUserId = $commentModel->UserId;
				}else{
					$messageBoxModel->ToUserId = $commentModel->UnregUserId;
				}
				break;
			case self::MESSAGE_DATA_MESSAGE_TYPE_REPLY_COMMENT:
				if($isTrueUser){
					if(self::$userInfo['sso_user_public'] == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
						$messageBoxModel->FromUserId = self::$userInfo['_auth_user_id'];
					}
				}else{
					$messageBoxModel->FromUserId = self::$userInfo['_auth_user_id'];
				}
				$messageBoxModel->ToUserId = $commentModel->ReplyUserId;
				$messageData['FromNickName'] = self::$userInfo['_auth_nick_name'];
				$messageData['CommentId'] = $commentModel->Id;
				break;		
			case self::MESSAGE_DATA_MESSAGE_TYPE_PARISE_COMMENT:
				if($isTrueUser){
					if(self::$userInfo['sso_user_public'] == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
						$messageBoxModel->FromUserId = self::$userInfo['_auth_user_id'];
					}
					$messageBoxModel->ToUserId = $commentModel->UserId;
				}else{
					$messageBoxModel->FromUserId = self::$userInfo['_auth_user_id'];
					$messageBoxModel->ToUserId = $commentModel->UnregUserId;
				}
				$messageData['FromNickName'] = self::$userInfo['_auth_nick_name'];
				break;
			default:
				break;
	
		}
		$messageData['Content'] = $messageDataContent;
		$messageBoxModel->MessageData = json_encode($messageData);
		$messageBoxModel->CreateTime = new \DateTime();
		return $messageBoxModel;
	}

	public static function formatCommentContent($content){
		if($content && preg_match_all('/<img[^<]*\>/i',$content,$match)){
			$content = preg_replace('/\<img[^<]*\>/','',$content).'[图]';
		}
		return $content;
	}
	public static function getCommentObjectData($objectId,$commentTypeCode){
		$objectInfoArr = array('Title'=>'','Url'=>'');
		switch ($commentTypeCode){
			case CommentTypeModel::TYPE_CODE_ARTICLE:
				$isArticle = false;
				if(self::$objectData['articleList']){
					if(isset(self::$objectData['articleList'][$objectId])){
						$articleInfo = self::$objectData['articleList'][$objectId];
						$objectInfoArr['Title'] = $articleInfo['Title'];
						$objectInfoArr['Url'] = $articleInfo['Url'];
						$isArticle = true;
					}
				}
				if(!$isArticle){
					// 造型日历处理
					if($objectId == \haibao\cms\business\control\pc\channel\calendar\CalendarController::CALENDAR_COMMENT){
						$objectInfoArr['Title'] = '造型日历';
						$objectInfoArr['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR);
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_PIC:
				if(self::$objectData['imageCollList']){
					if(isset(self::$objectData['imageCollList'][$objectId])){
						$imageCollInfo = self::$objectData['imageCollList'][$objectId];
						$objectInfoArr['Title'] = $imageCollInfo['Title'];
						$objectInfoArr['Url'] = $imageCollInfo['Url'];
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_VIDEO:
				if(self::$objectData['videoList']){
					if(isset(self::$objectData['videoList'][$objectId])){
						$videoInfo = self::$objectData['videoList'][$objectId];
						$objectInfoArr['Title'] = $videoInfo->Title;
						$objectInfoArr['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE).'/play/'.$videoInfo->Id.'.htm';
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_BRAND:
				if(self::$objectData['brandList']){
					if(isset(self::$objectData['brandList'][$objectId])){
						$brandInfo = self::$objectData['brandList'][$objectId];
						if($brandInfo->NameCN){
							$objectInfoArr['Title'] = $brandInfo->NameCN;
						} else {
							$objectInfoArr['Title'] = $brandInfo->NameEN;
						}
						$objectInfoArr['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).'/brand/'.$brandInfo->BrandUrl.'/';
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_STAR:
				if(self::$objectData['starList']){
					if(isset(self::$objectData['starList'][$objectId])){
						$starInfo = self::$objectData['starList'][$objectId];
						if($starInfo->NameCn){
							$objectInfoArr['Title'] = $starInfo->NameCn;
						} else {
							$objectInfoArr['Title'] = $starInfo->NameEn;
						}
						$objectInfoArr['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_STARS).'/star/'.$starInfo->TopicId.'/';
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_DESIGNER:
				if(self::$objectData['designerList']){
					if(isset(self::$objectData['designerList'][$objectId])){
						$designerInfo = self::$objectData['designerList'][$objectId];
						if($designerInfo->NameCN){
							$objectInfoArr['Title'] = $designerInfo->NameCN;
						} else {
							$objectInfoArr['Title'] = $designerInfo->NameEN;
						}
						$objectInfoArr['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_DESIGNER).'/designer/'.$designerInfo->TopicId.'/';
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_STAR_PK:
				if(self::$objectData['starpkList']){
					if(isset(self::$objectData['starpkList'][$objectId])){
						$starpkInfo = self::$objectData['starpkList'][$objectId];
						$objectInfoArr['Title'] = $starpkInfo['Title'];
						$objectInfoArr['Url'] = $starpkInfo['Url'];
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_FEATURE:
				if(self::$objectData['featureList']){
					if(isset(self::$objectData['featureList'][$objectId])){
						$featureInfo = self::$objectData['featureList'][$objectId];
						$objectInfoArr['Title'] = $featureInfo->FeatureDefine->FeatureName;
						$objectInfoArr['Url'] = $featureInfo->Url;
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_HB_HOME_BLOG:
				if(self::$objectData['blogList']){
					if(isset(self::$objectData['blogList'][$objectId])){
						$blogInfo = self::$objectData['blogList'][$objectId];
						$objectInfoArr['Title'] = $blogInfo['subject'];
						$objectInfoArr['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU).'/blog/'.$blogInfo['blogid'].'.htm';
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_MEIJU_INFO:
				if(self::$objectData['americanList']){
					if(isset(self::$objectData['americanList'][$objectId])){
						$americanInfo = self::$objectData['americanList'][$objectId];
						$objectInfoArr['Title'] = $americanInfo['Title'];
						$objectInfoArr['Url'] = $americanInfo['Url'];
					}
				}
				break;
			case CommentTypeModel::TYPE_CODE_IMAGE_LIBRARY:
			    if(self::$objectData['imageListList']){
			        if(isset(self::$objectData['imageListList'][$objectId])){
			            $americanInfo = self::$objectData['imageListList'][$objectId];
			            $objectInfoArr['Title'] = $americanInfo['Title'];
			            $objectInfoArr['Url'] = $americanInfo['Url'];
			        }
			    }
			    break;
		    case CommentTypeModel::TYPE_CODE_FASHION_WEAR:
		        if(self::$objectData['fashionWearList']){
		            if(isset(self::$objectData['fashionWearList'][$objectId])){
		                $americanInfo = self::$objectData['fashionWearList'][$objectId];
		                $objectInfoArr['Title'] = $americanInfo['Title'];
		                $objectInfoArr['Url'] = $americanInfo['Url'];
		            }
		        }
		        break;
			/* case CommentTypeModel::TYPE_CODE_SNS_ANYTOPIC:
				$objectInfoArr['Title'] = '造型日历';
				$objectInfoArr['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU);
				break; */
			default :
				break;
		}
		return $objectInfoArr;
	}
	
	/**
	 * 获取消息数据(消息盒子初始化/加载更多)
	 * @param Object $messageUser
	 * @param Integer $messageType
	 * @param Integer $minMessageId
	 * @return Array
	 */
	public static function getMessageList($messageUser,$messageType,$minMessageId=0,$isPc=true){
		if(!$isPc) self::$isPc = $isPc;
		$showAllUnReadMessage=false;
		if($messageUser->UserPublic == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
			$messageBoxFilter = new MessageBoxFilter();
			self::$messageBoxData = new MessageBoxData();
		}else{
			$messageBoxFilter = new MessageBoxUnRegFilter();
			self::$messageBoxData = new MessageBoxUnRegData();
		}
		if(!$minMessageId){
			switch($messageType){
				case MessageBoxModel::MESSAGE_TYPE_COMMENT:
					if($messageUser->UnReadCommentCount >= self::MESSAGE_LIMIT){
						$showAllUnReadMessage=true;
					}
					break;
				case MessageBoxModel::MESSAGE_TYPE_PRIVATE:
					if($messageUser->UnReadPrivateCount >= self::MESSAGE_LIMIT){
						$showAllUnReadMessage=true;
					}
					break;				
				case MessageBoxModel::MESSAGE_TYPE_SYSTEM:
					
					// 重新拉取一下系统的消息
					if($messageUser->UserPublic == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
						MessageSystemBusiness::syncMessage($messageUser->UserId);
					}
					if($messageUser->UnReadSystemCount >= self::MESSAGE_LIMIT){
						$showAllUnReadMessage=true;
					}
					break;			
			}
		}
		$messageBoxFilter->order(MessageBoxFilter::CONDITION_FIELD_NAME_CREATETIME,false);
		$messageBoxFilter->order(MessageBoxFilter::CONDITION_FIELD_NAME_ID,false);
		$messageBoxFilter->select(array(
				MessageBoxFilter::CONDITION_FIELD_NAME_ID,
				MessageBoxFilter::CONDITION_FIELD_NAME_MESSAGE_DATA,
				MessageBoxFilter::CONDITION_FIELD_NAME_CREATETIME,
				MessageBoxFilter::CONDITION_FIELD_NAME_MESSAGE_TYPE,
				MessageBoxFilter::CONDITION_FIELD_NAME_FROM_USER_ID,
				MessageBoxFilter::CONDITION_FIELD_NAME_TO_USER_ID,
				MessageBoxFilter::CONDITION_FIELD_NAME_IS_READ,
		));
		if($showAllUnReadMessage){
			$messageBoxFilter->where(MessageBoxFilter::CONDITION_FIELD_NAME_IS_READ,Condition::CONDITION_EQUAL,MessageBoxModel::READ_NOT);
		}else{
			$messageBoxFilter->limit(0,self::MESSAGE_LIMIT);
			if($minMessageId){
				$messageBoxFilter->where(MessageBoxFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_LESS,(int)$minMessageId);
			}
		}

		if($messageUser->UserPublic == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
			$messageBoxFilter->where(MessageBoxFilter::CONDITION_FIELD_NAME_MESSAGE_TYPE,Condition::CONDITION_EQUAL,(int)$messageType);
		}
		$messageBoxFilter->where(MessageBoxFilter::CONDITION_FIELD_NAME_TO_USER_ID,Condition::CONDITION_EQUAL,$messageUser->UserId);
		$messageList =  self::$messageBoxData->getAll($messageBoxFilter);
		if($messageList){
			$messageList = self::processMessageList($messageList,$messageUser,$messageType);
		}
		return $messageList;
	}
	
	/**
	 * 处理消息列表数据
	 * @param Array $messageList
	 * @param Object $messageUser
	 * @param Integer $messageType
	 */
	public static function processMessageList($messageList,$messageUser,$messageType){
		$messageIdArr = $dataArr = array();
		$messageCount = 0;
		foreach($messageList as $key=>$message){
			if($message->IsRead == MessageBoxModel::READ_NOT){
				$messageCount++;
				array_push($messageIdArr,$message->Id);
			}

			$messageData = json_decode($message->MessageData);
			switch($message->MessageType){
				case MessageBoxModel::MESSAGE_TYPE_COMMENT:
					if(isset($messageData->CommentsTypeCode)) {
						if(!isset($dataArr[$messageData->CommentsTypeCode])) {
							$dataArr[$messageData->CommentsTypeCode]=array();
						}
						array_push($dataArr[$messageData->CommentsTypeCode],$messageData->ObjectId);
					}
					$message->MessageData = $messageData;
					break;
				case MessageBoxModel::MESSAGE_TYPE_PRIVATE:
					$message->MessageData = $messageData;
					break;
				case MessageBoxModel::MESSAGE_TYPE_SYSTEM:
					$message->MessageData = $messageData;
					break;
			}
		}
		if($dataArr){
			self::getAllInfoContent($dataArr);
			
			foreach($messageList as $key=>$message){
				if($message->MessageType == MessageBoxModel::MESSAGE_TYPE_COMMENT){
					if(!isset($message->MessageData->CommentsTypeCode)) {
						$message->MessageData->Title = '';
						$message->MessageData->Url = '';
					}else{
						$objectData = self::getCommentObjectData($message->MessageData->ObjectId,$message->MessageData->CommentsTypeCode);
						$message->MessageData->Title = $objectData['Title'];
						$message->MessageData->Url = $objectData['Url'];
					}
				}
			}
		}
		if($messageCount>0){
			$messageCount = 0-$messageCount;
			if($messageUser->UserPublic == Auth::SSO_USER_PUBLIC_USER){
				$isTrueUser = true;
			}else{
				$isTrueUser = false;
			}
			self::$messageBoxData->updateMessageReadStatus($messageIdArr,MessageBoxModel::READ_HAVE);
			MessageUser::updateMessageUser($messageUser->UserId, $messageType, $isTrueUser,$messageCount);
		}
		return $messageList;
	}
	
	/**
	 * 更新message状态
	 * @param Array $messageIdArr
	 */
	public static function updateMessageReadStatus($messageIdArr,$status = MessageBoxModel::READ_HAVE,$isTrueUser=true){
		if(empty($messageIdArr)){
			throw new BusinessException('消息数据为空');
		}
		if($isTrueUser){
			$data = new MessageBoxData();
		}else{
			$data = new MessageBoxUnRegData();
		}
		$data->updateMessageReadStatus($messageIdArr,$status);
	}
	
	/**
	 * 定时获取消息数据
	 * @param Object $messageUser
	 * @param Integer $messageType
	 * @param Integer $minMessageId
	 */
	public static function getMessageListTimeing($messageUser,$messageType,$maxMessageId){
	
		if($messageUser->UserPublic == \haibao\comment\common\Auth::SSO_USER_PUBLIC_USER){
			$messageBoxFilter = new MessageBoxFilter();
			self::$messageBoxData = new MessageBoxData();
		}else{
			$messageBoxFilter = new MessageBoxUnRegFilter();
			self::$messageBoxData = new MessageBoxUnRegData();
		}
		$messageBoxFilter->order(MessageBoxFilter::CONDITION_FIELD_NAME_CREATETIME,false);
		$messageBoxFilter->select(array(
				MessageBoxFilter::CONDITION_FIELD_NAME_ID,
				MessageBoxFilter::CONDITION_FIELD_NAME_MESSAGE_DATA,
				MessageBoxFilter::CONDITION_FIELD_NAME_CREATETIME,
				MessageBoxFilter::CONDITION_FIELD_NAME_MESSAGE_TYPE,
				MessageBoxFilter::CONDITION_FIELD_NAME_FROM_USER_ID,
				MessageBoxFilter::CONDITION_FIELD_NAME_IS_READ,
		));
		$messageBoxFilter->where(MessageBoxFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_GREATER,(int)$maxMessageId);
		$messageBoxFilter->where(MessageBoxFilter::CONDITION_FIELD_NAME_MESSAGE_TYPE,Condition::CONDITION_EQUAL,(int)$messageType);
		$messageBoxFilter->where(MessageBoxFilter::CONDITION_FIELD_NAME_TO_USER_ID,Condition::CONDITION_EQUAL,$messageUser->UserId);
		$messageList =  self::$messageBoxData->getAll($messageBoxFilter);
		if($messageList){
			$messageList = self::processMessageList($messageList,$messageUser,$messageType);
		}
		return $messageList;
	}
	
	/**
	 * 用户数据合并
	 */
	public static function mergeUserData($userId,$unRegUserId){
		if(!$userId){
			throw new BusinessException('用户ID不能为空！');
		}
		if(!$unRegUserId){
			throw new BusinessException('游客用户ID不能为空！');
		}
		
		$data = new MessageBoxUnRegData();
		$data->mergeUserData($userId,$unRegUserId);
	}
	public static function getArtcileList($publishIds){
		
		$finalArr = array();
		$publishIds = array_flip(array_flip($publishIds));
		$articleFilter = new \haibao\cms\model\filter\Article();
		$articleTagFilter = new \haibao\cms\model\filter\ArticleTag();
		$articleFilter->select(array(
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_TITLE,
		));
		$articleTagFilter->select(array(
				\haibao\cms\model\filter\ArticleTag::CONDITION_FIELD_NAME_PARENT_ID,
		));
		$articleFilter->where(\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,Condition::CONDITION_IN,$publishIds);
		$articleTagFilter->where(\haibao\cms\model\filter\ArticleTag::CONDITION_FIELD_NAME_IS_PRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\ArticleTag::PRIMAIY_IS);
		$articleFilter->innerJoin($articleTagFilter,null,\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\ArticleTag::CONDITION_FIELD_NAME_ARTICLE_ID,\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_TAG,false);

		$data = new \haibao\cms\data\Article();
		$articleList = $data->getAll($articleFilter);
		if($articleList){
			foreach($articleList as $list){
				$finalArr[$list->PublishId]['Title'] = $list->Title;
				$parentTagId = $list->ArticleTag->ParentId;

				if(isset(self::$togTags[$parentTagId])){
					if(self::$isPc){
						$finalArr[$list->PublishId]['Url'] = self::$togTags[$parentTagId]['channelurl'].'/article/'.$list->PublishId.'.html';
					}else{
						$finalArr[$list->PublishId]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/'.self::$togTags[$parentTagId]['domain'].'/article/'.$list->PublishId.'.html';
					}
				}else{
					if(self::$isPc){
						$finalArr[$list->PublishId]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/article/'.$list->PublishId.'.html';
					}else{
						$finalArr[$list->PublishId]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/www/article/'.$list->PublishId.'.html';
					}
				}
			}
		}
		return $finalArr;
	}
	
	public static function getImageCollList($imageCollIds){

		$finalArr = array();
		$imageCollIds = array_flip(array_flip($imageCollIds));
		$imageCollFilter = new \haibao\cms\model\filter\imagecollective\ImageCollective();
		$imageCollTagFilter = new \haibao\cms\model\filter\imagecollective\ImageCollectiveTag();
		$imageCollFilter->select(array(
			\haibao\cms\model\filter\imagecollective\ImageCollective::CONDITION_FIELD_NAME_ID,
			\haibao\cms\model\filter\imagecollective\ImageCollective::CONDITION_FIELD_NAME_TITLE
		));
		$imageCollTagFilter->select(array(
				\haibao\cms\model\filter\imagecollective\ImageCollectiveTag::CONDITION_FIELD_NAME_TAG_PARENTID
		));
		$imageCollFilter->where(\haibao\cms\model\filter\imagecollective\ImageCollective::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$imageCollIds);
		$imageCollTagFilter->where(\haibao\cms\model\filter\imagecollective\ImageCollectiveTag::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY);
		$imageCollFilter->innerJoin($imageCollTagFilter,null,\haibao\cms\model\filter\imagecollective\ImageCollective::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\imagecollective\ImageCollectiveTag::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,\haibao\cms\model\filter\imagecollective\ImageCollective::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_TAG,false);
		$data = new \haibao\cms\data\imagecollective\ImageCollective();
		$imageCollList = $data->getAll($imageCollFilter);
		if($imageCollList){
			foreach($imageCollList as $list){
				$finalArr[$list->Id]['Title'] = $list->Title;
				$parentTagId = $list->ImageCollectiveTag->ParentId;
				if(isset(self::$togTags[$parentTagId])){
					if(self::$isPc){
						$finalArr[$list->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/'.self::$togTags[$parentTagId]['domain'].'/pics/'.$list->Id.'.html';
					}else{
						$finalArr[$list->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/'.self::$togTags[$parentTagId]['domain'].'/pics/'.$list->Id.'.html';
					}
				}else{
					if(self::$isPc){
						$finalArr[$list->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/www/pics/'.$list->Id.'.html';
					}else{
						$finalArr[$list->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/www/pics/'.$list->Id.'.html';
					}
				}
			}
		}
		return $finalArr;
	}
	
	public static function getStarPkList($starpkIds){
		$finalArr = array();
		$starpkIds = array_flip(array_flip($starpkIds));
		$starpkFilter = new \haibao\cms\model\filter\starpk\StarPkDefine();
		$starpkFilter->select(array(
				\haibao\cms\model\filter\starpk\StarPkDefine::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\starpk\StarPkDefine::CONDITION_FIELD_NAME_TITLE
		));
		$starpkFilter->where(\haibao\cms\model\filter\starpk\StarPkDefine::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$starpkIds);
		$data = new \haibao\cms\data\starpk\StarPkDefine();
		$starpkList = $data->getAll($starpkFilter);
		if($starpkList){
			foreach($starpkList as $list){
				$finalArr[$list->Id]['Title'] = $list->Title;
				$finalArr[$list->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_STARS).'/starpk/'.$list->Id.'/';	
			}
		}
		return $finalArr;
	}
	
	public static function getBlogList($blogIds){
		$finalArr = array();
		$blogIds = array_flip(array_flip($blogIds));
		$data = new \haibao\cms\data\control\BlogController();
		return $data->getBlogInfoByBlogIds($blogIds);
		
	}
	
	public static function getAmericanList($americanIds){
		$finalArr = array();
		$americanIds = array_flip(array_flip($americanIds));
		$americanFilter = new \haibao\cms\model\filter\americandrama\AmericanDefine();
		$americanFilter->select(array(
				\haibao\cms\model\filter\americandrama\AmericanDefine::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\americandrama\AmericanDefine::CONDITION_FIELD_NAME_NAMECN
		));
		$americanFilter->where(\haibao\cms\model\filter\americandrama\AmericanDefine::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$americanIds);
		$data = new \haibao\cms\data\americandrama\AmericanDefine();
		$americanList = $data->getAll($americanFilter);
		if($americanList){
			foreach($americanList as $list){
				$finalArr[$list->Id]['Title'] = $list->NameCH;
				$finalArr[$list->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_TV_HOST).'meiju/'.$list->Id.'/';
			}
		}
		return $finalArr;
	}
	public static function getImageLibraryList($imageIds,$isMessage = false){
	    $finalArr = array();
	    $imageIds = array_flip(array_flip($imageIds));
	    $imageLibraryFilter = new \haibao\cms\model\filter\ImageLibrary();
	    $imageLibraryFilter->select(array(
	        \haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_ID,
	        \haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_DESCRIPTION
	    ));
	    $imageLibraryFilter->where(\haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$imageIds);
	    $imageLibraryFilter->where(\haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\ImageLibrary::STATUS_PUBLISH);
	    $list = \haibao\cms\business\ImageLibrary::getImageAllByFilter($imageLibraryFilter);
	    if($list){
	        foreach ($list as $k=>$v){
	            $finalArr[$v->Id]['Title'] = $v->Description?$v->Description:'图片库中的一张图片';
	            if($isMessage){
	                $finalArr[$v->Id]['Title'] = $v->Description?$v->Description:'图片库';
	            }
	            $finalArr[$v->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/image/'.$v->Id.'.html?type=1';
	        }
	    }
	    return $finalArr;
	}
	public static function getFashionWearList($fashionWearIds,$isMessage = false){
	    $finalArr = array();
	    $fashionWearIds = array_flip(array_flip($fashionWearIds));
	    $fashionWearFilter = new \haibao\cms\model\filter\fashionwear\FashionWear();
	    $fashionWearFilter->select(array(
	        \haibao\cms\model\filter\fashionwear\FashionWear::CONDITION_FIELD_NAME_ID,
	        \haibao\cms\model\filter\fashionwear\FashionWear::CONDITION_FIELD_NAME_STARID,
	        \haibao\cms\model\filter\fashionwear\FashionWear::CONDITION_FIELD_NAME_USERDEFINEDSTARNAME
	    ));
	    $celebrityFilter = new \haibao\cms\model\filter\Celebrity();
	    $celebrityFilter->select(array(
	        \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,
	        \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_CN,
	        \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_EN
	    ));
	    $fashionWearFilter->where(\haibao\cms\model\filter\fashionwear\FashionWear::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$fashionWearIds);
	    $fashionWearFilter->leftJoin($celebrityFilter,null,\haibao\cms\model\filter\fashionwear\FashionWear::CONDITION_FIELD_NAME_STARID,\haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\fashionwear\FashionWear::CONDITION_FIELD_NAME_RELATED_INFO,false);
	    $fashionWearFilter->where(\haibao\cms\model\filter\fashionwear\FashionWear::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\fashionwear\FashionWear::STATUS_ENABLE);
	    $list = \haibao\cms\business\fashionwear\FashionWear::getFashionWearByFilter($fashionWearFilter);
	    if($list){
	        foreach ($list as $k=>$v){
	            $finalArr[$v->Id]['Title'] = ' 的一张图片';
	            if($isMessage){
	                $finalArr[$v->Id]['Title'] = '时尚穿搭';
	            }
	            if($v->RelatedInfo->NameCn && $v->RelatedInfo->NameEn){
	                $finalArr[$v->Id]['Title'] = $v->RelatedInfo->NameCn.'('.$v->RelatedInfo->NameEn.') 的一张图片';
	                if($isMessage){
	                    $finalArr[$v->Id]['Title'] = $v->RelatedInfo->NameCn.'('.$v->RelatedInfo->NameEn.')';
	                }
	            }elseif ($v->UserDefinedStarName){
	                $finalArr[$v->Id]['Title'] = $v->UserDefinedStarName.' 的一张图片';
	                if($isMessage){
	                    $finalArr[$v->Id]['Title'] = $v->UserDefinedStarName.'';
	                }
	            }
	            $finalArr[$v->Id]['Url'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/fashionimage/'.$v->Id.'.html';
	        }
	    }
	    return $finalArr;
	}
	public static function getAllInfoContent($dataArr,$isReturn=false){
		self::$togTags = \haibao\cms\business\control\Base::getTopTags();
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_ARTICLE])){
			self::$objectData['articleList'] = self::getArtcileList($dataArr[CommentTypeModel::TYPE_CODE_ARTICLE]);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_PIC])){
			self::$objectData['imageCollList'] = self::getImageCollList($dataArr[CommentTypeModel::TYPE_CODE_PIC]);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_FEATURE])){
			$featureIds = array_flip(array_flip($dataArr[CommentTypeModel::TYPE_CODE_FEATURE]));
			$featureList = \haibao\cms\business\FeaturePublish::getFeatureByIds($featureIds);
			foreach($featureList as $list){
				self::$objectData['featureList'][$list->Id] = $list;
			}
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_VIDEO])){
			$videoIds = array_flip(array_flip($dataArr[CommentTypeModel::TYPE_CODE_VIDEO]));
			self::$objectData['videoList'] = \haibao\cms\business\Video::getVideoByids($videoIds);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_BRAND])){
			$brandIds = array_flip(array_flip($dataArr[CommentTypeModel::TYPE_CODE_BRAND]));
			self::$objectData['brandList'] = \haibao\cms\business\Brand::getBrandByIds($brandIds);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_STAR])){
			$starIds = array_flip(array_flip($dataArr[CommentTypeModel::TYPE_CODE_STAR]));
			self::$objectData['starList'] = \haibao\cms\business\Celebrity::getStarByIds($starIds);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_DESIGNER])){
			$designerIds = array_flip(array_flip($dataArr[CommentTypeModel::TYPE_CODE_DESIGNER]));
			self::$objectData['designerList'] = \haibao\cms\business\Designer::getDesignerByIds($designerIds);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_STAR_PK])){
			self::$objectData['starpkList'] = self::getStarPkList($dataArr[CommentTypeModel::TYPE_CODE_STAR_PK]);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_HB_HOME_BLOG])){
			self::$objectData['blogList'] = self::getBlogList($dataArr[CommentTypeModel::TYPE_CODE_HB_HOME_BLOG]);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_MEIJU_INFO])){
			self::$objectData['americanList'] = self::getAmericanList($dataArr[CommentTypeModel::TYPE_CODE_MEIJU_INFO]);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_IMAGE_LIBRARY])){
		    self::$objectData['imageListList'] = self::getImageLibraryList($dataArr[CommentTypeModel::TYPE_CODE_IMAGE_LIBRARY],$isMessage = true);
		}
		if(isset($dataArr[CommentTypeModel::TYPE_CODE_FASHION_WEAR])){
		    self::$objectData['fashionWearList'] = self::getFashionWearList($dataArr[CommentTypeModel::TYPE_CODE_FASHION_WEAR],$isMessage = true);
		}
		if($isReturn){
			return self::$objectData;
		}
	}
	/**
	 * 
	 * @param Integer $fromUserId 来自于哪个用户
	 * @param Integer $toUserId 给哪个用户
	 * @param Array $messageData 消息数据  Array('FromNickName','Title','Content','InfoUrl','MessageType')
	 * @param Inter $messageType
	 */
	public static function addUserMessage($fromUserId,$toUserId,$messageData,$messageType = MessageBoxModel::MESSAGE_TYPE_COMMENT){
	

		$messageBoxModel = new MessageBoxModel();
		$messageData['IsTrueUser'] = true;
		$messageBoxModel->FromUserId = $fromUserId;
		$messageBoxModel->ToUserId = $toUserId;
		$messageBoxModel->MessageType = $messageType;
		$messageBoxModel->MessageData = json_encode($messageData);
		$messageBoxModel->CreateTime = new \DateTime();
		$data = new MessageBoxData;
		$data->add($messageBoxModel);
		MessageUserBusiness::updateMessageUser($messageBoxModel->ToUserId,MessageBoxModel::MESSAGE_TYPE_COMMENT,true,1);
	
	}
	
	public static  function dealMessageData($messageList){
	    $userArray = array();
	    foreach($messageList as $item){
	        array_push($userArray, $item->FromUserId, $item->ToUserId);
	    }
	     
	    if($userArray){
	        $nicknameArray = \haibao\user\business\User::getNickNameByUserIds($userArray);
	    }
	    foreach ($messageList as $key=>$value){
	         
	        $fromUserName = array_key_exists($value->FromUserId, $nicknameArray) ? $nicknameArray[$value->FromUserId] : null;
	        $fromUrl = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_USER).'/space/'.$value->FromUserId.'/';
	        $fromUserName = ' <span class="name"><a class="msgc-red" href="'.$fromUrl.'" target="_blank">'.$fromUserName.'</a></span>';
	         
	        switch ($value->MessageData->MsgType){
	             
	            case UserDynamicsModel::MESSAGE_TYPE_ATTENTION:
	                if($value->MessageData->InfoType == FavoriteModel::ATTENTION_TYPE_STAR){
	                    $value->Content = $fromUserName.'<span class="p8">关注了明星</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ATTENTION_TYPE_BRAND){
	                    $value->Content = $fromUserName.'<span class="p8">关注了品牌</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ATTENTION_TYPE_BLOGGER){
	                    $value->Content = $fromUserName.'<span class="p8">关注了博主</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ATTENTION_TYPE_DESIGNER){
	                    $value->Content = $fromUserName.'<span class="p8">关注了设计师</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ATTENTION_TYPE_AMERICAN){
	                    $value->Content = $fromUserName.'<span class="p8">关注了美剧</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ATTENTION_TYPE_USER){
	                    $value->Content = $fromUserName.'<span class="p8">关注了用户</span>';
	                }
	                break;
	            case UserDynamicsModel::MESSAGE_TYPE_COLLECTION:
	                if($value->MessageData->InfoType == FavoriteModel::ID_TYPE_AID_NUM){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了文章</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ID_TYPE_PID_NUM){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了图集</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ID_TYPE_SUBJECT_NUM){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了专题</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ID_TYPE_VIDEO_NUM){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了视频</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ID_TYPE_TRY){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了试用</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ID_TYPE_THREAD_NUM){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了帖子</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ID_TYPE_BLOG_NUM){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了博客</span>';
	                }elseif($value->MessageData->InfoType == FavoriteModel::ID_TYPE_CALANDER_NUM){
	                    $value->Content = $fromUserName.'<span class="p8">收藏了造型日历</span>';
	                }
	                break;
	            case UserDynamicsModel::MESSAGE_TYPE_COMMENT:
	                $value->Content = $fromUserName.'<span class="p8">评论了</span>';
	                break;
	            case UserDynamicsModel::MESSAGE_TYPE_APPLY_TRY:
	                $value->Content = $fromUserName.'<span class="p8">申请了试用</span>';
	                break;
	            case UserDynamicsModel::MESSAGE_TYPE_BLOG:
	                $value->Content = $fromUserName.'<span class="p8">发布了博客</span>';
	                break;
	            case UserDynamicsModel::MESSAGE_TYPE_BBS:
	                $value->Content = $fromUserName.'<span class="p8">发布了帖子</span>';
	                break;
	            case UserDynamicsModel::MESSAGE_TYPE_COMMENT_PRAISE:
	                $value->Content = $fromUserName.'<span class="p8">赞了</span>';
	                break;
	            default :
	                $value->Content = '';
	                break;
	        }
	         
	    }
	
	    return $messageList;
	}
	public static function getMessageListByType($messageBoxFilter){
	   $data = new MessageBoxData();
	   return $data->getAll($messageBoxFilter);
	    
	}
}